/**
 * 存股宝
 */
import Vue from 'vue'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

const importUserExcelResource = Vue.resource(API_ROOT + '/cgb/trade/import')
const userCertificateListResource = Vue.resource(API_ROOT + '/cgb/trade/list')
const sentCertificateResource = Vue.resource(API_ROOT + '/cgb/send/push')
const sentPriceResource = Vue.resource(API_ROOT + '/cgb/send/award')
const userExcelListResource = Vue.resource(API_ROOT + '/cgb/award/list')
const getCGBListResource = Vue.resource(API_ROOT + '/cgb/list')
const addCGBResource = Vue.resource(API_ROOT + '/cgb/add')
const updateCGBResource = Vue.resource(API_ROOT + '/cgb/update')
const deleteCGBResource = Vue.resource(API_ROOT + '/cgb/delete')
const swapCGBResource = Vue.resource(API_ROOT + '/cgb/swap')

// 凭证
export const importUserExcel = ({dispatch}, params) => {
  return importUserExcelResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('DATAFLOW_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
export const sentCertificate = ({dispatch}, params) => {
  return sentCertificateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('DATAFLOW_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const userCertificateList = ({dispatch}, params) => {
  return userCertificateListResource.save(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
// 发放奖品
export const getUserListExcel = ({dispatch}, params) => {
  return userExcelListResource.save(params)
    .then((res) => {
      dispatch('DATAFLOW_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const sentPrice = ({dispatch}, params) => {
  return sentPriceResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('DATAFLOW_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
// 获取存股宝列表
export const getCGBList = ({dispatch}, params) => {
  return getCGBListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
// 新增存股宝
export const addCGB = ({dispatch}, params) => {
  return addCGBResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
// 更新存股宝
export const updateCGB = ({dispatch}, params) => {
  return updateCGBResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
// 删除存股宝
export const deleteCGB = ({dispatch}, params) => {
  return deleteCGBResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
// 移动存股宝
export const swapCGB = ({dispatch}, params) => {
  return swapCGBResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('CGB_LIST_NEED_FRESH', 1)
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}
